﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MkLink;
using SymbolicLinkManageCore;
using SymbolicLinkManageUI.Windows;

namespace SymbolicLinkManageUI.Lib
{

    internal partial class SearchSettingsVM:ObservableObject
    {
        public static ObservableCollection<DriveLetterVM> DesignData = [new DriveLetterVM("a:"), new DriveLetterVM("b:"), new DriveLetterVM("c:"), new DriveLetterVM("d:"), new DriveLetterVM("e:"), new DriveLetterVM("f:"), new DriveLetterVM("a:"), new DriveLetterVM("b:"), new DriveLetterVM("c:"), new DriveLetterVM("d:"), new DriveLetterVM("e:"), new DriveLetterVM("f:")];

        private SearchSettingsWindow SearchWindow;
        
        [ObservableProperty]
        private ObservableCollection<DriveLetterVM> _DriveLetters = new ObservableCollection<DriveLetterVM>( MkLinkTools.LocalPathRoots.Select(p => new DriveLetterVM(p)).ToArray());
        public SearchSettingsVM() { SearchWindow = null!; }
        public SearchSettingsVM(SearchSettingsWindow window) { SearchWindow = window; }
        [ObservableProperty,NotifyCanExecuteChangedFor(nameof(SelectPathCommand))]
        private bool _IsEnablePathMode = false;

        [ObservableProperty]
        private string _Path = string.Empty;

        [ObservableProperty]
        private bool _IsEnableHardLink = false;

        [RelayCommand(CanExecute = nameof(IsEnablePathMode))]
        public void SelectPath()
        {
            if (Tools.OpenFolderDialog(SearchWindow, Environment.GetFolderPath(Environment.SpecialFolder.MyComputer), "选择要扫描的目录", true, out string path))
                Path = path;
        }

        [RelayCommand]
        public void Cancel() => SearchWindow.DialogResult = false;

        [RelayCommand]
        public void Submit()
        {
            if (IsEnablePathMode)
            {
                SearchWindow.IsEnableHardLink = IsEnableHardLink;
                SearchWindow.Result = [Path];
            }
            else
            {
                SearchWindow.IsEnableHardLink = IsEnableHardLink;
                SearchWindow.Result = DriveLetters.Where(p => p.IsSelect).Select(p => p.DriveLetter).ToArray();
            }
            SearchWindow.DialogResult = true;
        }
    }
}
